<?php
namespace apps\manage\controllers;

use Yii;
use services\core\FunctionMenuService;

/**
 * 菜单列表
 */
class MenuController extends BaseController
{
    public function actionList(){
        $functionMenuService = new FunctionMenuService([
            'platformCode' => 'manage'
        ]);
        $parentId = Yii::$app->getRequest()->get('parent_id', 0);
        return $functionMenuService->getMenuTree($parentId);
    }

    public function save($id = null){
        $functionMenuService = new FunctionMenuService([
            'platformCode' => 'manage'
        ]);
        $request = Yii::$app->getRequest();
        return $functionMenuService->save($request->post(), $id);
    }

    public function actionItem(){
        $id = Yii::$app->getRequest()->get('id', 0);
        if(!$id){
            return $this->fail('id必传');
        }
        $functionMenuService = new FunctionMenuService([
            'platformCode' => 'manage'
        ]);
        
        return $functionMenuService->getItem($id);
    }

    /**
     * 添加菜单.
     *
     * @parent manager/add
     * @return string
     */
    public function actionAdd()
    {
        return $this->save();
    }

    /**
     * 修改菜单.
     *
     * @parent manager/update
     * @return string
     */
    public function actionUpdate()
    {
        $request = Yii::$app->getRequest();
        $id = $request->get('id', null) ?: $request->post('id', null);
        return $this->save($id); 
     
    }

    public function actionDelete(){
        $id = Yii::$app->getRequest()->get('id', 0);
        if(!$id){
            return $this->fail('id必传');
        }
        $functionMenuService = new FunctionMenuService([
            'platformCode' => 'manage'
        ]);
        
        return $functionMenuService->getDelete($id);
    }
}
